package com.lib.online.apects;

import net.sf.ehcache.CacheManager;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LibLogger {
	private Logger infoLog = Logger.getLogger("com.lib.online.debug");
	private Logger errorLog = Logger.getLogger("com.lib.online.error");
	private Logger dbLog = Logger.getLogger("com.lib.online.db");

    @After("execution(* com.lib.online.service.*.*(..))")
    public void log(JoinPoint point) {
    	 if (infoLog.isDebugEnabled()) {
    		 infoLog.debug(point.getSignature().getName()  +  " called...");
    		 dbLog.debug(point.getSignature().getName()  +  " called...");
    	 }
    }
    
    @AfterThrowing(pointcut = "execution(* com.lib.online..*(..))", throwing = "ex")
    public void errorInterceptor(Exception ex) {
        if (errorLog.isEnabledFor(Priority.WARN)) {
        	errorLog.warn("Error Message Interceptor started");
        	errorLog.warn(ex.getClass() + " :   " + ex.getMessage() + ex.getCause().getMessage());
        	 if (ex.getCause().getClass().equals(org.hibernate.exception.ConstraintViolationException.class)) 
             	errorLog.warn("Cache violations");
        }
        
       
        	              //TODO DO SOMETHING HERE WITH EX
    }

}
